//
//  GetTopLikeSpotTask.m
//  ZooNgonDoiOS
//
//  Created by Anh Jun Linh Tinh on 7/5/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "GetTopLikeSpotTask.h"
#import "ZooSpotBean.h"
@implementation GetTopLikeSpotTask

-(id)initWithParams:(int)limit offset:(int)offset lon:(NSString *)lon lat:(NSString *)lat{
    self = [super init];
    if(self){
        _limit = limit;
        _offset = offset;
        _lon = lon;
        _lat = lat;
    }
    return self;
}

-(id)initWithParams:(int)limit lon:(NSString *)lon lat:(NSString *)lat{
    self = [self initWithParams:limit offset:0 lon:lon lat:lat];
    return self;
}

-(id)initWithParams:(NSString *)lon lat:(NSString *)lat{
    self = [self initWithParams:0 offset:0 lon:lon lat:lat];
    return self;
}

-(id)initWithParams:(int)limit{
    self = [self initWithParams:limit offset:0 lon:nil lat:nil];
    return self;
}

-(id)initWithParams:(int)limit offset:(int)offset{
    self = [self initWithParams:limit offset:offset lon:nil lat:nil];
    return self;
}

-(void)doExecute{
    NSMutableArray *arrayKeys = [[NSMutableArray alloc]init];
    NSMutableArray *arrayValues = [[NSMutableArray alloc] init];
    BOOL hasParam = NO;
    
    if(_limit > 0){
        [arrayKeys addObject:@"limit"];
        [arrayKeys addObject:@"offset"];
        
        NSString* strLimit = [NSString stringWithFormat:@"%i", _limit];
        NSString* strOffset = [NSString stringWithFormat:@"%i", _offset];
        [arrayValues addObject:strLimit];
        [arrayValues addObject:strOffset];
        hasParam = YES;
        
    }
    
    if(_lon != nil && _lat !=nil){
        [arrayKeys addObject:@"lon"];
        [arrayKeys addObject:@"lat"];
        
        [arrayValues addObject:_lon];
        [arrayValues addObject:_lat];
        hasParam = TRUE;
    }
    
    if(hasParam== TRUE){
        NSDictionary *params = [NSDictionary dictionaryWithObjects:arrayValues forKeys:arrayKeys];
        [[RKObjectManager sharedManager].client get:@"/spot/toplike" queryParameters:params delegate:self];
    }else{
        [[RKObjectManager sharedManager].client get:@"/spot/toplike" delegate:self];
    }
}

-(void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error{
    
}

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response{
    NSMutableArray *result = [[NSMutableArray alloc] init];
    NSString *jsonString = [response bodyAsString];
    NSDictionary *json = [jsonString objectFromJSONString];
    NSArray *datas = [json objectForKey:@"data"];
    int size = [datas count];
    if(size == 0){
        //call back method
        return;
    }
    
    for(int i=0;i<size;i++){
        NSDictionary *item = [datas objectAtIndex:i];
        ZooSpotBean *spotBean = [[ZooSpotBean alloc] init];
        spotBean.id_spot = [item objectForKey:@"id"];
        spotBean.name = [item objectForKey:@"name"];
        spotBean.lon = [item objectForKey:@"lon"];
        spotBean.lat = [item objectForKey:@"lat"];
        [result addObject:spotBean];
    }
    //call back retrun result;(NSMutableArray)
}

@end
